package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    /**
     * 菜品条件分页查询
     * @param query
     * @return
     */
    @Override
    public PageResult selectPage(DishPageQueryDTO query) {
        //1.开启分页
        PageHelper.startPage(query.getPage(), query.getPageSize());

        List<Dish> page=dishMapper.selectPage(query);
        Page<Dish> p=(Page<Dish>) page;
        return new PageResult(p.getTotal(),p.getResult());
    }

    /**
     * 回显
     * 根据id查询菜品
     */
    @Override
    public DishVO getById(Long id) {
        //1.查询菜品数据
        DishVO dishVo = dishMapper.selectById(id);
        //2.查询菜品口味数据
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        dishVo.setFlavors(flavors);
        return dishVo;
    }

    /**
     * 修改菜品
     * @param dish
     */
    @Transactional
    @Override
    public void updateById(DishDTO dishDTO) {
        //先修改菜品的基本信息
        dishMapper.updateById(dishDTO);

        //把id填充到每个flavors中
        for (DishFlavor flavor : dishDTO.getFlavors()) {
            flavor.setDishId(dishDTO.getId());
        }
        //修改菜品的口味
        //删除所有口味
        dishFlavorMapper.deleteById(dishDTO.getId());
        //新增口味
        dishFlavorMapper.insertDishFlavor(dishDTO.getFlavors());
    }

    /**
     * 根据分类id查询菜品
     * 批量查询菜品
     * @param id
     * @return
     */
    @Override
    public List<Dish> getListById(Long categoryId) {
        return dishMapper.getListById(categoryId);
    }



    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    public void deleteByIds(List<Integer> ids) {
        dishMapper.deleteByIds(ids);
    }

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void insertDish(DishDTO dishDTO) {
        //新增菜品的基本信息
        dishDTO.setCreateTime(LocalDateTime.now());
        dishDTO.setUpdateTime(LocalDateTime.now());
        dishMapper.insertDish(dishDTO);

        //把菜品的id设置到dishDTO中
        for (DishFlavor flavor : dishDTO.getFlavors()) {
            flavor.setDishId(dishDTO.getId());
        }
        //新增菜品的口味
        dishFlavorMapper.insertDishFlavor(dishDTO.getFlavors());
    }

    /**
     * 菜品起售、停售
     * 改变菜品的状态
     */
    @Override
    public void updateStatusById(Long id) {
        dishMapper.updateStatusById(id);
    }
}
